var app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    myData:[], 
   cardArray:[],
   id:''
  },
  // twoTitle: function (res) {
  //   // console.log(res.detail.id)
  //   var id = res.detail.id
  //   for (var index = 0; index < this.data.myData.length; index++) {
  //     if (this.data.myData[index].id == id) {
  //       this.setData({
  //         twoData: this.data.myData[index].arr
  //       })

  //     }
  //   }
  // },
  // twoTitle:function(event){
  //    var id=event.detail.id
  //    wx.navigateTo({
  //      url: '../eduTable/eduTab?id='+id,
  //    })
  // },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     var id=options.id
     this.setData({
       id:id
     })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },
  downData:function(event){
    console.log(event)
    var url=event.detail.url
    wx.showLoading({
      title: '正在下载',
    })
    wx.downloadFile({
      url:url,
      // filePath:'/storage/sdcard0/Download',
      header:{
        'cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID')
      },
      success:function(res){
        wx.hideLoading()
        wx.showToast({
          title: '下载成功',
          icon:'none'
        })
        console.log(res)
        
                        
        
      },
      fail:function(res){
        wx.hideLoading()
        wx.showToast({
          title:'下载资料失败',
          icon:'none'
        })
      }
    })
  },
  onlineRead:function(event){
    console.log(event)
    wx.showLoading({
      title: '正在打开文件中，请勿做其他操作',
      mask:true
    })
    var url=event.detail.url
    var type=event.detail.type
    wx.downloadFile({
      url: url,
      header: {
        'cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID')
      },
      success: function (res) {
        console.log(res)
        // wx.showToast({
        //   title: '下载资料成功'
        // })
        var filePath=res.tempFilePath
        wx.openDocument({
          filePath: filePath,
          fileType:type,
          success:function(res){
            console.log(res)
            wx.hideLoading()
          }
        })
      },
      fail: function (res) {
        wx.showToast({
          title: '下载资料失败',
          icon: 'none'
        })
      }
    })
  },
  getBook:function(event){  
      console.log(event)
      var that=this
      var price=event.currentTarget.dataset.price
      var id=event.currentTarget.dataset.id
      var type=event.type
      var buy=event.currentTarget.dataset.buy
      var title
      var fun
      if(type=='down'){
         title='下载'
        fun='down'
      }else{
       title='在线阅读'
       fun='read'
      }
      var vip=wx.getStorageSync('vip')
      if(buy){
         if(type=='down'){
           that.downData(event)
         }else{
           that.onlineRead(event)
         }
      }else if(!vip){
        wx.showModal({
          title: '',
          content: '是否愿意花费' + price + "积分" + title + "资料",
          confirmColor: '#02a0e9',
          success: function (res) {
            console.log(id)
            console.log(price)
            if (res.confirm) {
              wx.request({
                url: app.globalData.baseUrl + 'resource/buy',
                method: 'POST',
                data: {
                  resourceId: id,
                  price: price
                },
                header: {
                  'cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID'),
                  'content-type': 'application/x-www-form-urlencoded'
                },
                success: function (res) {
                  console.log('购买成功')
                  console.log(res)
                  if (res.data.code == 0) {
                    if (fun == 'down') {
                      that.downData(event)
                    } else {
                      that.onlineRead(event)
                    }
                  }else{
                    console.log('购买失败')
                  }
                },
                fail: function (err) {
                  wx.showModal({
                    title: '请求错误',
                    content: '请检查是够连接网络',
                  })
                }

              })


            }
          }
        })
      }else{
        wx.request({
          url: app.globalData.baseUrl + 'resource/buy',
          method: 'POST',
          data: {
            resourceId: id,
            price: price
          },
          header: {
            'cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID'),
            'content-type': 'application/x-www-form-urlencoded'
          },
          success:function(res){
            if (fun == 'down') {
              that.downData(event)
            } else {
              that.onlineRead(event)
            }
          },
          fail:function(res){
            if (fun == 'down') {
              that.downData(event)
            } else {
              that.onlineRead(event)
            }
          }
        })
      }
   
     
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.request({
      url: app.globalData.baseUrl+'resource/listcatalog',
      method: 'GET',
      header: {
        'cookie': 'JSESSION=' + wx.getStorageSync('sessionID')
      },
      success: function (res) {
        for (var i = 0; i < res.data.data.data.length; i++) {
          var id = 'myData[' + i + '].id'
          var name = 'myData[' + i + '].title'
          var arr = 'myData[' + i + '].arr'
          
          that.setData({
            [id]: res.data.data.data[i].id,
            [name]: res.data.data.data[i].name,
           
            // [arr]: res.data.data.data[i].catalogTwoList
          })

        }
      }
      })
    var that=this
     wx.request({
       url: app.globalData.baseUrl+'resource/listcattwo',
       method:'GET',
       header:{
         'cookie':'JSESSIONID='+wx.getStorageSync('sessionID')
       },
       data:{
         catalogTwoId:that.data.id
       },
       success:function(res){
         console.log(res)
         var resData=res.data.data.data
         try{
           for (var i = 0; i < resData.length; i++) {
             var type = 'cardArray[' + i + '].type'
             var title = 'cardArray[' + i + '].title'
             var price = 'cardArray[' + i + '].price'
             var description = 'cardArray[' + i + '].description'
             var buy='cardArray['+i+'].buy'
             var id='cardArray['+i+'].id'
             var fileUrl=resData[i].file.split('.')
             var len=fileUrl.length-1
             var url='cardArray['+i+'].url'
            //  console.log(fileUrl)
             var name = resData[i].name
             if(name.length>16){
               name=name.substring(0,16)+'...'
             }else{
               name=name
             }
             var des = resData[i].description
             console.log(des.length)
             if(des.length>19){
               des=des.substring(0,16)+'...'
             }
             
             that.setData({
               [id]:resData[i].id,
               [type]: fileUrl[len],
               [buy]:resData[i].isBought,
               [title]: name,
               [price]:resData[i].price,
               [description]:des,
               [url]:resData[i].file
            })
           }
         }catch(err){
           console.log(err)
         }
        
       }
     })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    onPullDownRefresh
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})